package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class RegistrationFormServlet
 */
@WebServlet("/RegistrationFormServlet")
public class RegistrationFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String FIRST_NAME = "firstName";
	public static final String LAST_NAME = "lastName";
	public static final String EMAIL_ADDRESS = "emailAddress";
       
    public RegistrationFormServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//
		String actionURL = RegistrationServlet.class.getSimpleName();
		//
		String firstName = CookieUtilities.getCookieValue(request, FIRST_NAME, "");
		String lastName = CookieUtilities.getCookieValue(request, LAST_NAME, "");
		String emailAddress = CookieUtilities.getCookieValue(request, EMAIL_ADDRESS, "");
		
		//
		String title = "Please Register.";
		out.println(Constants.docType + ServletUtilities.headWithTitle(title) +
				"<body><h1>" + title + "</h1><form action=\"" + actionURL +
				"\" method=\"get\"><br/>First Name :<input type=\"text\" name=\"" +
				FIRST_NAME + "\" value=\"" + firstName +"\"/><br/>\n" +
				"Last Name: <input type=\"text\" name=\"" + LAST_NAME + "\" value=\"" + 
				lastName + "\"/><br/>" + 
				"Email-Address: <input type=\"text\" name=\"" + EMAIL_ADDRESS + "\" value=\"" + 
				emailAddress + "\"/><br/>" + 
				"<input type = \"submit\" value=\"Register\" />\n</form></body></html>"
				);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private static final Logger logger = 
			Logger.getLogger(RegistrationFormServlet.class);

}
